/*
 *  HTTP/TCP and FTP traffic simulation model from permanent
 *  document IEEE 802.16.3c-01/30r1 of the IEEE 802.16 committee
 *
 *  C.Cicconetti, University of Pisa, 2006.
 */

#ifndef ns_bwa_internet_h
#define ns_bwa_internet_h

#include <stdlib.h>
#include <random.h>
#include <trafgen.h>
#include <ranvar.h>

//! Traffic model source.
class BwaInternet : public TrafficGenerator {
	//! Fixed number of IPP processes (four).
	enum { N_IPP = 4 };

public:
	//! Create an empty traffic generator.
	BwaInternet ();
	//! Do nothing.
	virtual ~BwaInternet () { }

	//! Tcl interface.
	int command(int argc, const char*const* argv);

	//! Compute the next talkspurt and silence period duration.
	virtual double next_interval(int&);
	//! Called when the previous burst expired.
	virtual void timeout();
	//! HACK so that udp agent knows interpacket arrival time within a burst.
	inline double interval() {
		return (packetRate_[current_] > 0.0 ) ? 1.0 / packetRate_[current_] : 0; }

protected:
	//! Packet size. Configured via Tcl. Default 192 bytes.
	unsigned int packetSize_;

	//! Traffic rate. Configured via Tcl. Default 100 kb/s.
	double rate_;

	//! Start traffic generation (called from the TCL scripts).
	virtual void start();

	//! Initialize packet generation.
	virtual void init ();

	//! Packet rate during ON processes.
	double packetRate_[N_IPP];

	//! Number of packets for each process.
	unsigned int burstSize_[N_IPP];

	//! Next interval for each process.
	double nextInterval_[N_IPP];

	//! Burst currently served.
	int current_;

	//! Interrupted Poisson Process: ON interval duration.
	ExponentialRandomVariable ipp_on_[N_IPP];
	//! Interrupted Poisson Process: OFF interval duration.
	ExponentialRandomVariable ipp_off_[N_IPP];

	//! Return the index of the smallest value in an array.
	unsigned int smallest (double x[N_IPP]);

	//! File for printing out traces.
	FILE* trace_;
};

#endif // ns_bwa_internet_h
